﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SdmxMl.UC;

namespace SdmxMl.Common
{
    public class AnnotationLocator
    {
        public Annotation Annot { get; set; }
        public string UrnContainer { get; set; }
        public string Representatation { get; set; }


        public AnnotationLocator(Annotation a, string urn, string repres)
        {
            Annot = a;
            UrnContainer = urn;
            Representatation = repres;

        }
    }

    public class AnnotationLocatorList : List<AnnotationLocator>
    {
        private string typePattern;
        private SearchType typeSearchType;

        private string textPattern;
        private SearchType textSearchType;

        private string titlePattern;
        private SearchType titleSearchType;

        private string urnPattern;
        private SearchType urnSearchType;

        public AnnotationLocatorList()
        {
            init(string.Empty, string.Empty, string.Empty, string.Empty);
        }

        public AnnotationLocatorList(string type, string txt, string title, string urn) : base(16)
        {
            init(type, txt, title, urn);
        }

        private void init(string type, string txt, string title, string urn)
        {
            typePattern = type == null ? string.Empty : type;
            typeSearchType = Search.DefinePattern(ref typePattern);

            textPattern = txt == null ? string.Empty : txt;
            textSearchType = Search.DefinePattern(ref textPattern);

            titlePattern = title == null ? string.Empty : title;
            titleSearchType = Search.DefinePattern(ref titlePattern);

            urnPattern = urn == null ? string.Empty : urn;
            urnSearchType = Search.DefinePattern(ref urnPattern);
        }

        public bool IsUndefinedPattern()
        {
            return typePattern.Length == 0 && textPattern.Length == 0 && titlePattern.Length == 0 && urnPattern.Length == 0;
        }

        public bool isCandidate(Annotation ann)
        {
            bool candidate = true;

            if (typePattern.Length > 0)
                candidate = Search.MatchPattern(typeSearchType, typePattern, ann.Type);

            if (candidate && textPattern.Length > 0)
                candidate = Search.MatchPattern(textSearchType, textPattern, ann.Text.ToString());

            if (candidate && titlePattern.Length > 0)
                candidate = Search.MatchPattern(titleSearchType, titlePattern, ann.Title);

            if (candidate && urnPattern.Length > 0)
            {
                if (ann.Url == null)
                    candidate = false;
                else
                    candidate = Search.MatchPattern(urnSearchType, urnPattern, ann.Url.ToString());
            }

            return candidate;
        }

        #region Table Representation

        /// <summary>Presents annotations as datatable</summary>
        /// <returns>a datatable representation</returns>
        public DataTable AsDataTable()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("Type", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Title", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Text", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Urn", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Owner", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Reference", typeof(string)));

            foreach (AnnotationLocator ann in this)
                dataTable.Rows.Add(new object[] {ann.Annot.Type, ann.Annot.Title, ann.Annot.Text.ToString(),
                ann.Annot.Url == null ? string.Empty : ann.Annot.Url.ToString(), ann.Representatation, ann.UrnContainer});

            return dataTable;
        }
        #endregion


    }

}
